/*MyPhotoIndex Free Photo Organizer <www.myphotoindex.com>
  Copyright (C) <2006-2009>  <Asaf Yarkoni asaf@yarkoni.net>

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.*/
using System;
using System.Collections.Generic;
using System.Text;
using MyPhotoIndex.Interfaces;
using MyPhotoIndex.Controls.TagTree;
using MyPhotoIndex.DataAccess;
using MyPhotoIndex.ImageBank;
using MyPhotoIndex.Interfaces.Imaging;
using MyPhotoIndex.Interfaces.Tagging;
using MyPhotoIndex.Utilities;

namespace MyPhotoIndex
{
    class SelectedTagsCollection : IImageCollection
    {
        Dictionary<int, TagsImageData> m_tagMap = new Dictionary<int, TagsImageData>();
        private List<IImageData> m_imageDataList = new List<IImageData>();

        public SelectedTagsCollection()
        {
            Refresh();
        }

        public void Refresh()
        {
            m_tagMap.Clear();
            ImageTagCollection tagFactory = ImageTagCollection.Instance;
            List<IImageTag> tagList = tagFactory.Tags;
            BuildTagTree(tagList);

            List<IImageData> oldList = new List<IImageData>(m_imageDataList);
            m_imageDataList.Clear();
            foreach (IImageData imageData in oldList)
            {
                TagsImageData newImageData = null;
                if (m_tagMap.TryGetValue(imageData.Id, out newImageData) == true)
                {
                    m_imageDataList.Add(newImageData);
                }
            }
        }

        private void BuildTagTree(List<IImageTag> tagList)
        {
            foreach (IImageTag tag in tagList)
            {
                m_tagMap.Add(tag.Id, new TagsImageData(tag));
                if (tag.ChildTags != null)
                {
                    BuildTagTree(tag.ChildTags);
                }
            }
        }

        #region IImageCollection Members

        public IImageData this[int imageId]
        {
            get
            {
                return m_tagMap[imageId];
            }
        }

        public int Count
        {
            get
            {
                return m_imageDataList.Count;
            }
        }

        #endregion

        public void Add(int tagId)
        {
            try
            {
                IImageData imageData = m_tagMap[tagId];
                if (m_imageDataList.Contains(imageData) == false)
                {
                    m_imageDataList.Add(imageData);
                }
            }
            catch(Exception ex)
            {
                Logger.Log(ex);
            }
        }

        public void Remove(int tagId)
        {
            m_imageDataList.Remove(m_tagMap[tagId]);
        }

        public void Remove(int imageId, bool deleteFromMedia)
        {
            throw new NotImplementedException(Resources.Resources.MethodIsNotImplemented);
        }

        internal void Clear()
        {
            m_imageDataList.Clear();
        }

        public IImageData[] ToArray()
        {
            return m_imageDataList.ToArray();
        }

        #region IEnumerable<ImageData> Members

        public IEnumerator<IImageData> GetEnumerator()
        {
            return m_imageDataList.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        #endregion

        #region IImageCollection Members

        public event EventHandler<EventArgs> ImageCollectionLoadedEvent;

        #endregion

        #region IDisposable Members

        public void Dispose()
        {

        }

        #endregion
    }
}
